<?php

/**
 * app api account
 */
class action extends app
{
	public function display()
	{
		$action = $this->ev->url(3);
		if(!method_exists($this,$action))
			$action = "index";
		exit($this->$action());
	}

	/**
	 * API登陆
	 * @author macbren <14987037@qq.com>
	 * @param string username 用户名
	 * @param string userpassword 密码
	 * @return string
	 */
	public function login()
	{
		$session = $this->session->getSessionValue();
		if(TIME - $session['sessionlasttime'] < 1)
			return json_encode(array('status' => 'fail', 'code' => 300, 'message' => '操作失败'));
		$user = $this->user->getUserByUserName($this->ev->get('username'));
		if(!$user['userid'])
			return json_encode(array('status' => 'fail', 'code' => 300, 'message' => '用户不存在'));
		if($user['userpassword'] != md5($this->ev->get('userpassword')))
			return json_encode(array('status' => 'fail', 'code' => 300, 'message' => '用户名或密码错误'));
		$this->session->setSessionUser(array('sessionuserid'=>$user['userid'],'sessionpassword'=>$user['userpassword'],'sessionip'=>$this->ev->getClientIp(),'sessiongroupid'=>$user['usergroupid'],'sessionlogintime'=>TIME,'sessionusername'=>$user['username']));
		return json_encode(array(
			'status' => 'success',
			'code' => 0,
			'message' => '操作成功',
			'data' => array(
				'token' => $session['sessionid']
			))
		);
	}

	/**
	 * API注销
	 * @author macbren <14987037@qq.com>
	 * @param string token 令牌
	 * @return string
	 */
	public function logout()
	{
		$token = $this->ev->get('token');
		$session = $this->session->getSessionValue($token);
		if($session['sessionuserid'])
			return json_encode(array('status' => 'fail', 'code' => 300, 'message' => '操作失败'));
		$this->session->offOnlineUser($session['sessionuserid']);
		return json_encode(array('status' => 'success', 'code' => 0, 'message' => '操作成功'));
	}
}

?>
